package com.lq.educationsystem.lqclasses.service.impl;

import com.lq.educationsystem.common.util.*;
import com.lq.educationsystem.common.vo.ClassesStudentPageVo;
import com.lq.educationsystem.lqclasses.entiy.ClassesStudentTb;
import com.lq.educationsystem.lqclasses.mapper.ClassesStudentTbMapper;
import com.lq.educationsystem.lqclasses.service.ClassesStudentBaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassesStudentBaseImpl implements ClassesStudentBaseService {

    @Autowired
    ClassesStudentTbMapper classesStudentTbMapper;
    @Override
    public void insertInfo(ClassesStudentTb classesStudentInfo) {
        int row = classesStudentTbMapper.insertSelective(classesStudentInfo);
        System.out.println("classesStudent插入的数据为: "+row);
    }

    @Override
    public void deleteById(Integer classesStudentId) {
        int row = classesStudentTbMapper.deleteByPrimaryKey(Long.valueOf(classesStudentId));
        System.out.println("classesStudent删除的数据为: "+row);

    }

    @Override
    public void updateInfo(ClassesStudentTb classesStudentInfo) {
        int row = classesStudentTbMapper.updateByPrimaryKeySelective(classesStudentInfo);
        System.out.println("classesStudent更新的数据为: "+row);
    }

    @Override
    public Result queryById(Integer classesStudentId) {
        ClassesStudentTb classesStudentInfo = classesStudentTbMapper.selectByPrimaryKey(Long.valueOf(classesStudentId));
        return ResultBuild.buildOk(new Result<ClassesStudentTb>(),classesStudentInfo);
    }

    @Override
    public ResultPage<ClassesStudentTb> queryList(ClassesStudentPageVo classesStudentPageVo) {
        //list 列表
        int counts=1;
        System.out.println("classesStudentPageVo = " + classesStudentPageVo);
        //获取页面大小
        Integer page = classesStudentPageVo.getPage();
        Integer pageSize = classesStudentPageVo.getPageSize();
        int start = PageUtil.getStart(page, pageSize);


        //数据复制
        ClassesStudentTb classesStudentTb = new ClassesStudentTb();
        BeanUtils.copyProperties(classesStudentPageVo,classesStudentTb);

        List<ClassesStudentTb>  classesStudentTbList = classesStudentTbMapper.queryList(start,pageSize,classesStudentTb);
        //查总数

        counts = classesStudentTbMapper.queryCount(classesStudentTb);
        return ResultPageBuild.pageBuildOk(new ResultPage<ClassesStudentTb>(),page,pageSize,counts,classesStudentTbList);
    }

    @Override
    public ResultPage<ClassesStudentTb> queryClassesStudenInfotByStudentId(ClassesStudentPageVo classesStudentInfo) {
        ClassesStudentTb classesStudentTb = new ClassesStudentTb();
        //数据拷贝
        BeanUtils.copyProperties(classesStudentInfo,classesStudentTb);

        List<ClassesStudentTb> classesStudentTbList = classesStudentTbMapper.queryClassesStudenInfotByStudentId(classesStudentTb);

        //查询总数
        int counts = classesStudentTbMapper.queryCount(classesStudentTb);
        return ResultPageBuild.pageBuildOk(new ResultPage<ClassesStudentTb>(),classesStudentInfo.getPage(),classesStudentInfo.getPageSize(),counts,classesStudentTbList);
    }

    @Override
    public Result<ClassesStudentTb> queryClassesInfoByStudentId(ClassesStudentTb classesStudentInfo) {
        ClassesStudentTb classesStudentTb = classesStudentTbMapper.queryClassesInfoByStudentId(classesStudentInfo.getStudentId());
        return ResultBuild.buildOk(classesStudentTb);
    }

    /**
     * 通过班级id查询学生id
     * @param classesStudentPageVoList
     * @return
     */
    @Override
    public ResultPage<ClassesStudentTb> queryBatchClassesStudentInfoByStudentId(ClassesStudentPageVo classesStudentPageVoList) {
        ClassesStudentTb classesStudentTb = new ClassesStudentTb();
        BeanUtils.copyProperties(classesStudentPageVoList,classesStudentTb);
        //计算其实位置
        int start = PageUtil.getStart(classesStudentPageVoList.getPage(), classesStudentPageVoList.getPageSize());

        List<ClassesStudentTb> classesStudentTbList = classesStudentTbMapper.queryList(start, classesStudentPageVoList.getPageSize(), classesStudentTb);
        //获取信息集合
        //统计计数
        int counts = classesStudentTbMapper.queryCount(classesStudentTb);
        System.out.println("counts = " + counts);
        System.out.println("classesStudentPageVoList = " + classesStudentPageVoList);
        return ResultPageBuild.pageBuildOk(new ResultPage<ClassesStudentTb>(),classesStudentPageVoList.getPage(),classesStudentPageVoList.getPageSize(),counts,classesStudentTbList);
    }

    @Override
    public ResultPage<ClassesStudentTb> queryClassesStudentInfoListByClassesId(ClassesStudentPageVo classesStudentPageVo) {

        //计算起始位置
        Integer page = classesStudentPageVo.getPage();
        Integer pageSize = classesStudentPageVo.getPageSize();
        int start = PageUtil.getStart(page, pageSize);

        ClassesStudentTb classesStudentTb = new ClassesStudentTb();
        classesStudentTb.setClassesId(classesStudentPageVo.getClassesId());
        //班级id
        List<ClassesStudentTb> classesStudentTbList = classesStudentTbMapper.queryClassesStudentInfoListByClassesId(classesStudentTb,start,pageSize);

        //查询数量
        int counts = classesStudentTbMapper.queryCount(classesStudentTb);

        return ResultPageBuild.pageBuildOk(new ResultPage<ClassesStudentTb>(),page,pageSize,counts,classesStudentTbList);
    }

}
